package vip.xiaonuo.supplier.modular.purchase.suppliers.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PinyinUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.purchase.suppliers.entity.SupplierSuppliers;
import vip.xiaonuo.supplier.modular.purchase.suppliers.mapper.SupplierSuppliersMapper;
import vip.xiaonuo.supplier.modular.purchase.suppliers.param.SupplierSuppliersAddParam;
import vip.xiaonuo.supplier.modular.purchase.suppliers.param.SupplierSuppliersEditParam;
import vip.xiaonuo.supplier.modular.purchase.suppliers.param.SupplierSuppliersIdParam;
import vip.xiaonuo.supplier.modular.purchase.suppliers.param.SupplierSuppliersPageParam;
import vip.xiaonuo.supplier.modular.purchase.suppliers.service.SupplierSuppliersService;

import java.util.List;

/**
 * 供应商Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/02/28 21:58
 **/
@Service
public class SupplierSuppliersServiceImpl extends ServiceImpl<SupplierSuppliersMapper, SupplierSuppliers> implements SupplierSuppliersService {

    @Override
    public Page<SupplierSuppliers> page(SupplierSuppliersPageParam supplierSuppliersPageParam) {
        QueryWrapper<SupplierSuppliers> queryWrapper = new QueryWrapper<SupplierSuppliers>().checkSqlInjection();
//        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSupplierCode())) {
//            queryWrapper.lambda().like(SupplierSuppliers::getSupplierCode, supplierSuppliersPageParam.getSupplierCode());
//        }
//        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSupplierName())) {
//            queryWrapper.lambda().like(SupplierSuppliers::getSupplierName, supplierSuppliersPageParam.getSupplierName());
//        }
//        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSupplierPerson())) {
//            queryWrapper.lambda().like(SupplierSuppliers::getSupplierPerson, supplierSuppliersPageParam.getSupplierPerson());
//        }
//        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSupplierContact())) {
//            queryWrapper.lambda().like(SupplierSuppliers::getSupplierContact, supplierSuppliersPageParam.getSupplierContact());
//        }
//        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSupplierTel())) {
//            queryWrapper.lambda().like(SupplierSuppliers::getSupplierTel, supplierSuppliersPageParam.getSupplierTel());
//        }
        if(ObjectUtil.isNotEmpty(supplierSuppliersPageParam.getSearchKey())) {
            queryWrapper.and(q -> q.lambda().like(SupplierSuppliers::getSupplierCode, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierName, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierPinyin, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierPerson, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierContact, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierTel, supplierSuppliersPageParam.getSearchKey())
                    .or().like(SupplierSuppliers::getSupplierAddress, supplierSuppliersPageParam.getSearchKey())
            );
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierSuppliers::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierSuppliersPageParam.getSortField(), supplierSuppliersPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierSuppliersPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierSuppliersPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierSuppliersPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierSuppliers::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierSuppliersAddParam supplierSuppliersAddParam) {
        SupplierSuppliers supplierSuppliers = BeanUtil.toBean(supplierSuppliersAddParam, SupplierSuppliers.class);
        supplierSuppliers.setSupplierPinyin(PinyinUtil.getAllFirstLetter(supplierSuppliers.getSupplierName()));
        //数据归属
        supplierSuppliers.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierSuppliers);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierSuppliersEditParam supplierSuppliersEditParam) {
        SupplierSuppliers supplierSuppliers = this.queryEntity(supplierSuppliersEditParam.getId());
        BeanUtil.copyProperties(supplierSuppliersEditParam, supplierSuppliers);
        supplierSuppliers.setSupplierPinyin(PinyinUtil.getAllFirstLetter(supplierSuppliers.getSupplierName()));
        this.updateById(supplierSuppliers);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierSuppliersIdParam> supplierSuppliersIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierSuppliersIdParamList, SupplierSuppliersIdParam::getId));
    }

    @Override
    public SupplierSuppliers detail(SupplierSuppliersIdParam supplierSuppliersIdParam) {
        return this.queryEntity(supplierSuppliersIdParam.getId());
    }

    @Override
    public SupplierSuppliers queryEntity(String id) {
        SupplierSuppliers supplierSuppliers = this.getById(id);
        if(ObjectUtil.isEmpty(supplierSuppliers)) {
            throw new CommonException("供应商不存在，id值为：{}", id);
        }
        return supplierSuppliers;
    }
}
